/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.RoleManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.RoleCreateForm;
import info.peigen.hotpot.manage.core.manage.form.RoleEditForm;
import info.peigen.hotpot.manage.core.manage.form.RoleSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.RoleManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.RoleEntity;
import info.peigen.hotpot.manage.core.service.RoleService;
import info.peigen.hotpot.manage.core.service.order.RoleCreateOrder;
import info.peigen.hotpot.manage.core.service.order.RoleEditOrder;
import info.peigen.hotpot.manage.core.service.result.RoleCreateResult;
import info.peigen.hotpot.manage.core.service.result.RoleEditResult;
import info.peigen.hotpot.manage.core.service.result.info.RoleInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(RoleManageService)</b>
 * 管理后台 "角色" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class RoleManageService {
    @Inject
    RoleManageRepository roleManageRepository;
    @Inject
    RoleService          roleService;

    public PageResult<RoleInfo> page(RoleSearchForm form) {
        LambdaQuery<RoleEntity> lambdaQuery = query(form);
        return roleManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, RoleManageConvertor.INSTANCE::convertToInfo);
    }

    public List<RoleInfo> list(RoleSearchForm form) {
        LambdaQuery<RoleEntity> lambdaQuery = query(form);
        List<RoleEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(RoleManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public RoleInfo create(RoleCreateForm form) {
        RoleCreateOrder  order  = RoleManageConvertor.INSTANCE.convertToCreateOrder(form);
        RoleCreateResult result = roleService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public RoleInfo edit(RoleEditForm form) {
        RoleEditOrder  order  = RoleManageConvertor.INSTANCE.convertToEditOrder(form);
        RoleEditResult result = roleService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        roleManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        roleManageRepository.removeByIds(ids);
    }

    public RoleInfo detail(Integer id) {
        RoleEntity entity = roleManageRepository.getMapper().single(id);
        return RoleManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public RoleInfo detail(String roleId) {
        RoleEntity entity = roleManageRepository.getMapper().findByRoleId(roleId);
        return RoleManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<RoleEntity> query(RoleSearchForm form) {
        return roleManageRepository.getMapper().createLambdaQuery()
                .andLike(RoleEntity::getRoleId, Query.filterLikeEmpty(form.getRoleId(), true, true))
                .andLike(RoleEntity::getOrganizationId, Query.filterLikeEmpty(form.getOrganizationId(), true, true))
                .andLike(RoleEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}